﻿var cookieExpireDate = 60 * 60 * 24 * 30 * 1000;  //milliseconds : one month

$(document).ready(function () {
    $("#login").click(function () {
        var loginWindow = $(".LoginWindow");
        if (loginWindow.css("display") == "none") {
            var offset = $(this).offset();
            loginWindow.css("left", offset.left);
            $(".RegisterWindow").slideUp(200);
            loginWindow.slideDown(500);
        } else
            loginWindow.slideUp(200);
        return false;
    });

    $("#register").click(function () {
        var registerWindow = $(".RegisterWindow");
        if (registerWindow.css("display") == "none") {
            var offset = $(this).offset();
            registerWindow.css("left", offset.left);
            $(".LoginWindow").slideUp(200);
            registerWindow.slideDown(500);
        } else
            registerWindow.slideUp(200);
        return false;
    });


    $(".LoginWindow form").validate({
        rules: {
            Email: {
                required: true,
                minlength: 6,
                email: true
            },
            Password: {
                required: true,
                minlength: 6
            }
        },
        messages: {
            Email: "Invalid e-mail",
            Password: "Less than 6 letters"
        },
        submitHandler: function () {
            var email = $(".LoginWindow .Email").val();
            var pass = $.md5($(".LoginWindow .Password").val());
            $.ajax({
                type: "POST",
                url: "/Auth/Login",
                dataType: "json",
                data: "Email=" + email + "&Password=" + pass,
                success: function (data) {
                    if (data.success) {
                        var date = new Date(new Date().getTime() + cookieExpireDate);
                        setCookie("userID", data.userID, date.toUTCString(), "/");
                        setCookie("pass", pass, date.toUTCString(), "/");
                        window.location = $(".LoginWindow form").attr("action");
                    } else {
                        $(".LoginWindow form").addClass("LoginError");
                    }
                },
                error: function () {
                    alert("AJAX error occurred");
                }
            });
        }
    });


    $(".RegisterWindow form").validate({
        rules: {
            Email: {
                required: true,
                minlength: 6,
                email: true
            },
            Password: {
                required: true,
                minlength: 6
            },
            ConfirmPassword: {
                equalTo: ".RegisterWindow .Password"
            },
            Captcha: {
                required: true,
                minlength: 6,
                maxlength: 6
            },
        },
        messages: {
            Email: "Invalid e-mail",
            Password: "Less than 6 letters",
            ConfirmPassword: "Must equals to password",
            Captcha: "Must be 6 letters"
        },
        submitHandler: function () {
            var email = $(".RegisterWindow .Email").val();
            var pass = $.md5($(".RegisterWindow .Password").val());
            var confirmPass = $(".RegisterWindow .ConfirmPassword").val();
            var captcha = $(".RegisterWindow .Captcha").val();
            $.ajax({
                type: "POST",
                url: "/Auth/Register",
                dataType: "json",
                data: "Email=" + email + "&Password=" + pass + "&Captcha=" + captcha,
                success: function (data) {
                    if (data.success) {
                        var date = new Date(new Date().getTime() + cookieExpireDate);
                        setCookie("userID", data.userID, date.toUTCString(), "/");
                        setCookie("pass", pass, date.toUTCString(), "/");
                        window.location = $(".RegisterWindow form").attr("action");
                    } else {
                        var errors = ['Email already exists', 'Wrong captcha'];
                        $(".RegisterWindow .ErrorMessage").html(errors[data.messageCode]);
                        $(".RegisterWindow form").addClass("RegisterError");
                    }
                },
                error: function () {
                    alert("AJAX error occurred");
                }
            });
        }
    });
});